package com.efast.dmom.mgmt.service.mes.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.util.date.DateUtils;
import com.efast.dmom.mgmt.dao.mes.BsnRuleDao;
import com.efast.dmom.mgmt.entity.mes.BsnRule;
import com.efast.dmom.mgmt.service.mes.BsnRuleService;

@Service
public class BsnRuleServiceImpl implements BsnRuleService {
	
    @Autowired
	private BsnRuleService bsnRuleService;
    @Autowired
    private BsnRuleDao bsnRuleDao;
    //获取Bsn配置信息
	@Override
	public List<BsnRule> getBsnRuleInfo() {
		// TODO Auto-generated method stub
		return bsnRuleDao.getBsnRuleInfo();
	}
	//修改Bsn配置信息
	@Override
	public int updateBsnRuleInfoById(BsnRule enti) {
		// TODO Auto-generated method stub
		return bsnRuleDao.updateBsnRuleInfoById(enti);
	}
	//返回Bsn号
	@Override
	public String returnBsnNum() {
		// TODO Auto-generated method stub
		List<BsnRule>bsnRuleInfo=bsnRuleDao.getBsnRuleInfo();
		if(bsnRuleInfo.size()>0) {
    		//校验位 长度1
    		String CheckNo =bsnRuleInfo.get(0).getCheck_no();
    		//vin年份代码  长度1
    		String BsnYear =bsnRuleInfo.get(0).getBsn_year();
    		//装配厂 长度 1
    		String  AssemblyPlant =bsnRuleInfo.get(0).getAssembly_plant();
    		//流水号 长度 6
    		int ProNumber =bsnRuleInfo.get(0).getPro_number();
    		
    		StringBuffer bfBufferProNumber=new StringBuffer(String.valueOf(ProNumber));
    		bfBufferProNumber.reverse();
    		int size=6-bfBufferProNumber.length();
    		for(int i=0;i<size;i++) {
    			bfBufferProNumber.append("0");
    		}
    		bfBufferProNumber.reverse();
    		String bsnNum=CheckNo+BsnYear+AssemblyPlant+bfBufferProNumber;
    		BsnRule bsnRule=bsnRuleInfo.get(0);
    		bsnRule.setPro_number(bsnRule.getPro_number()+1);
    		bsnRule.setLastUpdateUser(SpringWebUtils.getRequestUser());
    		bsnRule.setCompanyCode(SpringWebUtils.getRequestCompany());
    		bsnRule.setLastUpdateTime(DateUtils.getCurrentDate());
    		bsnRuleDao.updateBsnRuleInfoById(bsnRule);
    		return bsnNum;
		}
		return null;
	}
	

}
